package com.pb.infra.auth.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pb.infra.auth.entity.custom.LedingOutFile;
import com.pb.infra.auth.entity.pbsys.FileDownload;
import com.pb.infra.auth.entity.pbsys.FileDownloadExample;
import com.pb.infra.auth.mapper.custom.LeadingOutMapper;
import com.pb.infra.auth.mapper.pbsys.FileDownloadMapper;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.JwtTokenConfig;
import com.pb.infra.oth.util.StringUtil;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 异步导出excel
 *
 * @Author:liaohaoyuan
 * @Date: 2021/04/26
 */
@Service
public class LeadingOutService {

    /**
     * 文件管理表DAO映射
     */
    @Resource
    private LeadingOutMapper leadingOutMapper;

    /**
     * 文件管理表DAO映射
     */
    @Resource
    private FileDownloadMapper fileDownloadMapper;

    /**
     * jwttoken配置类
     */
    @Resource
    private JwtTokenConfig jwtTokenConfig;

    /**
     * 保存临时文件数据
     * @param ledingOutFile
     */
    public void inset(LedingOutFile ledingOutFile){
        leadingOutMapper.insert(ledingOutFile);
    }

    /**
     * 查询所有离线数据
     *
     * @param request 查询参数
     * @return
     */
    public Result selectAllFile(HttpServletRequest request, Page page, Map<String, Object> paramMap){
        if (page != null && page.getPageIndex() != null && page.getPageSize() != null) {
            PageHelper.startPage(page.getPageIndex(), page.getPageSize());
        }
        String token = request.getHeader(jwtTokenConfig.getHeader());
        Claims claims = jwtTokenConfig.getClaimByToken(token);
        String userId = "";
        if (!StringUtil.isEmpty(claims)) {
            userId = (String)claims.get(jwtTokenConfig.getSub());
            FileDownloadExample fileDownloadExample = new FileDownloadExample();
            FileDownloadExample.Criteria criteria = fileDownloadExample.createCriteria();
            criteria.andFileUserEqualTo(userId);
            if (!StringUtil.isEmpty(paramMap.get("fileName"))) {
                criteria.andFileNameLike("%" + paramMap.get("fileName").toString() + "%");
            }
            fileDownloadExample.setOrderByClause("FILE_TIME desc");
            List<FileDownload> list = fileDownloadMapper.selectByExample(fileDownloadExample);
            return Result.ok(new PageInfo(list).getTotal(), list);
        } else {
            return Result.ok(new PageInfo(new ArrayList<>()).getTotal(), new ArrayList<>());
        }
    }

    /**
     * 删除三天之前的数据
     *
     * @param filePath     文件路径
     */
    public void deleteFile(String filePath){
        FileDownloadExample fileDownloadExample = new FileDownloadExample();
        FileDownloadExample.Criteria criteria = fileDownloadExample.createCriteria();
        criteria.andFilePathLike(filePath + "%");
        fileDownloadMapper.deleteByExample(fileDownloadExample);
    }
}
